﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Timers;
using System.Threading;

namespace homework_4
{
    /// <summary>
    /// 设置装载每秒被调用的方法的委托，
    /// 充当计时器Elapsed事件的Handler
    /// </summary>
    public delegate void SetControlValue();

    /// <summary>
    /// WindowForm类
    /// </summary>
    public partial class Form1 : Form
    {
        private SetSignal sig;

        private String outputInfo;
        private int counter;

        private System.Timers.Timer tmr;

        public Form1()
        {
            InitializeComponent();
            sig = new SetSignal();
            outputInfo = "";
            counter = 1;

            tmr = new System.Timers.Timer();
            tmr.Interval = 1000;
            tmr.Elapsed += (Object, EventArgs) =>
            {
                sig.SetDateAutoPerSeconds();
            };
            tmr.Elapsed += new ElapsedEventHandler(tickAction);
            tmr.Start();
        }

        /// <summary>
        /// 调用显示当前时间、滴答、检测闹钟的方法，
        /// 作为充当Timer计时器的Elapsed事件的handler的委托对象的方法参数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="handler"></param>
        private void tickAction(Object sender,EventArgs args)
        {
            SetControlValue handler = new SetControlValue(NowTimeText);
            handler += TickTack;
            handler += AlarmRing;
            this.Invoke(handler);

            //this.Invoke(new SetControlValue(NowTimeText));
            //this.Invoke(new SetControlValue(TickTack));
            //this.Invoke(new SetControlValue(AlarmRing));
        }

        /// <summary>
        /// 每秒对textBox1所显示的时间进行更新
        /// </summary>
        private void NowTimeText()
        {
            textBox1.Text = sig.Date.ToString("t");
        }

        /// <summary>
        /// 接收到每秒的滴答信号时，更新信息输出框的内容
        /// </summary>
        private void TickTack()
        {
            textBox1.Text = sig.Date.ToString("t");
            textBox4.Text = outputInfo + "滴答: " + counter + "次\r\n" ;
            counter++;
        }

        /// <summary>
        /// 接收到闹钟时间到时，更新信息输出框的内容
        /// </summary>
        private void AlarmRing()
        {
            if (sig.compareAlarm())
            {
                outputInfo = textBox4.Text;
                textBox4.Text = outputInfo + "\r\n闹钟响了！！！\r\n";
                counter = 1 ;
            }
        }

        /// <summary>
        /// 按钮—恢复系统时钟
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            textBox1.Text = System.DateTime.Now.ToString("t");
            textBox3.Text = "System time";
            sig.setDate("", true);
            outputInfo = textBox4.Text;
            outputInfo += "恢复为系统时间: " + sig.Date + "\r\n";
            counter = 1;
            textBox4.Text = outputInfo;
        }

        /// <summary>
        /// 按钮—重置当前时间为
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            if (sig.setDate(textBox3.Text, false))
            {
                outputInfo = textBox4.Text;
                outputInfo += "时间重置为: " + sig.Date + "\r\n";
            }
            else
            {
                outputInfo = textBox4.Text;
                outputInfo += "重置时间失败: \r\n";
            }
            counter = 1;
            textBox4.Text = outputInfo;
        }

        /// <summary>
        /// 按钮—关闭闹钟
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            sig.cancelAlarm();
            textBox2.Text = "blank";
            outputInfo = textBox4.Text;
            outputInfo += "闹钟已关闭\r\n";
            counter = 1;
            textBox4.Text = outputInfo;
        }

        /// <summary>
        /// 按钮—确认
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (!sig.setAlarm(textBox2.Text))
            {
                textBox2.Text = "failed";
                outputInfo = textBox4.Text;
                outputInfo += "闹钟设置失败\r\n";
                counter = 1;
                textBox4.Text = outputInfo;
                return;
            }
            outputInfo = textBox4.Text;
            outputInfo += "闹钟设置为: " + textBox2.Text + "\r\n";
            counter = 1;
            textBox4.Text = outputInfo;
        }

    }
}
